/*Emamul Islam Emon.      Id No: 093-15-844

5)	Example of  Link List. */

     	/* PROGRAM IMPLEMENTATION OF SINGLE LINKED LIST */

  #include<stdio.h>
  //#define NULL 0
  

	/* STRUCTURE CONTANING A DATA PART AND A LINK PART */

   struct node
 	{
   		int data;
   		struct node *next;
 	}*p;

  	

	/* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE FIRST NODE IN LIST */

 	/*THIS FUNCTION DELETES A NODE */

	
	delnode(int num)
 	{
     		struct node *temp, *m;
		temp=p;
      		while(temp!=NULL)
    		{
       			if(temp->data==num)
       			{
           			if(temp==p)
           			{
              				p=temp->next;
              				free(temp);
              				return;
           			}
           			else
        			{
           				m->next=temp->next;
           				free(temp);
           				return;
         			}
      			}
			else
        		{
           			m=temp;
          			temp= temp->next;
        		}

		}
		printf("ELEMENT %d NOT FOUND ",num);
	}



		/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */

    	append( int num )
	{
     		struct node *temp,*r;
     
		/* CREATING A NODE AND ASSIGNING A VALUE TO IT */

       		temp= (struct node *)malloc(sizeof(struct node));
       		temp->data=num;
       		r=(struct node *)p;

      		if (p == NULL) 				/* IF LIST IS EMPTY CREATE FIRST NODE */
     		{
    			p=temp;
         		p->next =NULL;
     		}
  		else
     		{        				/* GO TO LAST AND ADD*/

             		while( r->next != NULL)
       			r=r->next;
       			r->next =temp;
       			r=temp;
       			r->next=NULL;
     		}
  	}





		/* ADD A NEW NODE AT BEGINNING  */

     
	addbeg( int num )
   	{
        
		/*  CREATING A NODE AND INSERTING VALUE TO IT  */

   		struct node *temp;
        	temp=(struct node *)malloc(sizeof(struct node));
        	temp->data=num;

       		/* IF LIST IS NULL ADD AT BEGINNING  */

        	if ( p== NULL)
       		{
          		p=temp;
          		p->next=NULL;
       		}

   		else
      		{
          		temp->next=p;
          		p=temp;
      		}
   	}



	/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */


  	display(struct node *r)
  	{
      		r=p;
      		if(r==NULL)
     		{
       			printf("NO ELEMENT IN THE LIST :");
       			return;
     		}
       

		/* traverse the entire linked list */
       

		while(r!=NULL)
    		{
      			printf("%d ",r->data);
      			r=r->next;
    		}

     		printf("");
  	}


		//THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LIST
	count()
 	{
   		struct node *n;
   		int c=0;
   		n=p;
    		while(n!=NULL)
   		{
     			n=n->next;
     			c++;
   		}
  		return(c);
 	}



/* MAIN PROGRAM  */

  void main()
 {
        int i;
   	p=NULL;
   	while(1) /* this is an indefinite loop */
 	{
    		printf("1.INSERT A NUMBER AT BEGINNING;\n");
    		printf("2.INSERT A NUMBER AT LAST:\n");
    
    		printf("3.PRINT THE ELEMENTS IN THE LIST :\n");
    		printf("4.PRINT THE NUMBER OF ELEMENTS IN THE LIST:\n");
    		printf("5.DELETE A NODE IN THE LINKED LIST:\n");
    
    		printf("6.Exit\n");
    		printf("PLEASE, SELECT A OPTION (1 TO 6):\n");

    		scanf("%d",&i); /* ENTER A VALUE FOR SWITCH  */

      		switch(i)
    		{
         		case 1:
      			{
        			int num;
        			printf("PLEASE ENTER THE NUMBER :-");
        			scanf("%d",&num);
        			addbeg(num);
        			break;
      			}
          		case 2:
       			{
         			int num;
         			printf("PLEASE ENTER THE NUMBER :-");
         			scanf("%d",&num);
         			append(num);
         			break;
       			}

   			case 3:
      			{
         			struct node *n;
         			printf("THE  ELEMENTS IN THE LIST ARE : ");
         			display(n);
         			break;
      			}

      			case 4:
   			{
      				struct node *n;
      				display(n);
      				printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count());
      				break;
   			} 

			case 5:
    			{
            			int num;
      				printf("PLEASE DELETE A NUMBER FROM THE LIST :");
      				scanf("%d",&num);
      				delnode(num);
     				break;
    			}
   
			
  			case 6:
 			{
  				exit();
 			}
    		}/* end if switch */
 	}/* end of while */

getch();
}/* end of main */

